<template>
  <div class="websocket-test">
    <h1>WebSocket测试页面</h1>
    
    <div class="connection-status">
      <h3>连接状态: {{ connectionStatus }}</h3>
      <button @click="connect" :disabled="isConnected">连接</button>
      <button @click="disconnect" :disabled="!isConnected">断开</button>
    </div>
    
    <div class="test-messages">
      <h3>测试消息</h3>
      <div v-for="message in messages" :key="message.id" class="message">
        <span class="timestamp">{{ formatTime(message.timestamp) }}</span>
        <span class="content">{{ message.content }}</span>
      </div>
    </div>
    
    <div class="send-message">
      <input v-model="testMessage" placeholder="输入测试消息" />
      <button @click="sendMessage" :disabled="!isConnected">发送</button>
    </div>
  </div>
</template>

<script>
import webSocketService from '@/services/websocket'

export default {
  name: 'WebSocketTest',
  data() {
    return {
      connectionStatus: 'CLOSED',
      messages: [],
      testMessage: '',
      userId: '7' // 测试用户ID
    }
  },
  computed: {
    isConnected() {
      return this.connectionStatus === 'OPEN'
    }
  },
  mounted() {
    this.setupWebSocketListeners()
  },
  beforeUnmount() {
    this.cleanupWebSocketListeners()
  },
  methods: {
    setupWebSocketListeners() {
      webSocketService.on('connected', this.onConnected)
      webSocketService.on('disconnected', this.onDisconnected)
      webSocketService.on('taskNotification', this.onTaskNotification)
      webSocketService.on('error', this.onError)
    },
    
    cleanupWebSocketListeners() {
      webSocketService.off('connected', this.onConnected)
      webSocketService.off('disconnected', this.onDisconnected)
      webSocketService.off('taskNotification', this.onTaskNotification)
      webSocketService.off('error', this.onError)
    },
    
    connect() {
      webSocketService.connect(this.userId)
      this.updateConnectionStatus()
    },
    
    disconnect() {
      webSocketService.disconnect()
      this.updateConnectionStatus()
    },
    
    sendMessage() {
      if (this.testMessage.trim()) {
        webSocketService.send({
          type: 'test',
          message: this.testMessage,
          userId: this.userId
        })
        this.addMessage('发送: ' + this.testMessage)
        this.testMessage = ''
      }
    },
    
    onConnected() {
      this.addMessage('WebSocket连接已建立')
      this.updateConnectionStatus()
    },
    
    onDisconnected() {
      this.addMessage('WebSocket连接已断开')
      this.updateConnectionStatus()
    },
    
    onTaskNotification(notification) {
      this.addMessage(`收到任务通知: ${notification.taskId} - ${notification.status} - ${notification.message}`)
    },
    
    onError(error) {
      this.addMessage('WebSocket错误: ' + error.message)
      this.updateConnectionStatus()
    },
    
    updateConnectionStatus() {
      this.connectionStatus = webSocketService.getConnectionState()
    },
    
    addMessage(content) {
      this.messages.unshift({
        id: Date.now(),
        content,
        timestamp: new Date()
      })
      if (this.messages.length > 50) {
        this.messages = this.messages.slice(0, 50)
      }
    },
    
    formatTime(date) {
      return date.toLocaleTimeString()
    }
  }
}
</script>

<style scoped>
.websocket-test {
  padding: 20px;
  max-width: 800px;
  margin: 0 auto;
}

.connection-status {
  margin: 20px 0;
  padding: 15px;
  background: #f5f5f5;
  border-radius: 8px;
}

.connection-status h3 {
  margin: 0 0 10px 0;
}

.connection-status button {
  margin-right: 10px;
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.connection-status button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.test-messages {
  margin: 20px 0;
  max-height: 400px;
  overflow-y: auto;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 15px;
}

.message {
  margin: 8px 0;
  padding: 8px;
  background: #f9f9f9;
  border-radius: 4px;
  font-family: monospace;
}

.timestamp {
  color: #666;
  margin-right: 10px;
}

.send-message {
  margin: 20px 0;
  display: flex;
  gap: 10px;
}

.send-message input {
  flex: 1;
  padding: 8px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.send-message button {
  padding: 8px 16px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.send-message button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
</style>
